package dev.werillib.android

import android.content.Context

class SPUtil constructor(context: Context) {
    var mContext: Context = context

    fun setString(key: String, value: String?) {
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .edit()
            .putString(key, value)
            .apply()
    }

    fun getString(key: String, defaultValue: String = ""): String? =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .getString(key, defaultValue)

    fun setBoolean(key: String, value: Boolean) {
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .edit()
            .putBoolean(key, value)
            .apply()
    }

    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .getBoolean(key, defaultValue)

    fun setInt(key: String, value: Int) {
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .edit()
            .putInt(key, value)
            .apply()
    }

    fun getInt(key: String, defaultValue: Int = 0) =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .getInt(key, defaultValue)

    fun setStringSet(key: String, value: Set<String>) =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .edit()
            .putStringSet(key, value)
            .apply()

    fun getStringSet(key: String) =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .getStringSet(key, HashSet<String>())

    fun setLong(key: String, value: Long) =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .edit()
            .putLong(key, value)
            .apply()

    fun getLong(key: String, defaultValue: Long = 0L) =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .getLong(key, defaultValue)

    fun setFloat(key: String, value: Float) =
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .edit()
            .putFloat(key, value)
            .apply()

    fun getFloat(key: String, defaultValue: Float = 0F) {
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .getFloat(key, defaultValue)
    }

    fun remove(key: String) {
        mContext.getSharedPreferences(key, Context.MODE_PRIVATE)
            .edit()
            .remove(key)
            .apply()
    }
}